package com.hcrazy.manchester.app.controllers.seller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.CustomerSearchForm;
import com.hcrazy.manchester.app.form.OrderSearchForm;
import com.hcrazy.manchester.app.form.SellerSearchForm;
import com.hcrazy.manchester.app.pojo.BookOrder;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Seller;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.BookOrderService;
import com.hcrazy.manchester.app.services.FoodTypeService;
import com.hcrazy.manchester.app.services.SellerService;
import com.hcrazy.manchester.app.services.UserService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/seller")
public class OrderController {
	
	@Autowired
	private SellerService sellerService;
	
	@Autowired
	private FoodTypeService foodTypeService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private BookOrderService orderService;
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_customer",method=RequestMethod.POST)
	public String searchAddress(@ModelAttribute("form") CustomerSearchForm form,
			Model model,HttpServletRequest req){
		//是否执行删除操作
		if(form.getInfoId() > 0){
			
		}
		//重置infoId
		form.setInfoId(0);
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			model.addAttribute("message", "请首先登录系统！");
			return "/seller/failed";
		}
		User user = (User)object;
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllCustomersByUserId(form.getPageNum(), form.getSearch(),user.getUserId(),form.getSellerId());
		model.addAttribute("pageModel", pageModel);
		
		setSellers(model,user.getUserId());
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/seller/customer";
	}
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/customer",method=RequestMethod.GET)
	public String seller(Model model,HttpServletRequest req){
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			model.addAttribute("message", "请首先登录系统！");
			return "/seller/failed";
		}
		User user = (User)object;
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllCustomersByUserId(1,"",user.getUserId(),0);
		model.addAttribute("pageModel", pageModel);
		
		setSellers(model,user.getUserId());
		
		//初始化查询对象
		model.addAttribute("searchForm", new SellerSearchForm());
		
		return "/seller/customer";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllCustomersByUserId(int pageNum,String search,long userId,long sellerId){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);

		try {
			return orderService.getPagedCustomers(model, search, userId,sellerId,Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}

	private void setSellers(Model model,long userId){
		//根据功能码设置分类列表
		try {
			List<Seller> sellers = sellerService.getSellersByUserId(userId);
			model.addAttribute("sellers", sellers);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}		
	}
	
	/*******************************************orders******************************************************/
	
	/**
	 * 编辑信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/addOrder",method=RequestMethod.GET)
	public void addOrder(@RequestParam long orderId,Model model){
		try {
			//根据id获取信息内容
			BookOrder order = orderService.getOrderByOrderId(orderId);
			//更新返回对象
			model.addAttribute("order",order);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_order",method=RequestMethod.POST)
	public String searchOrder(@ModelAttribute("form") OrderSearchForm form,
			Model model,HttpServletRequest req){
		//是否执行删除操作
		if(form.getOrderId() > 0){
			
		}
		//重置infoId
		form.setOrderId(0);
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			model.addAttribute("message", "请首先登录系统！");
			return "/seller/failed";
		}
		User user = (User)object;
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllOrdersByUserId(form.getPageNum(), form.getSearch(),form.getSellerId(),form.getOrderDateStart(),form.getOrderDateEnd(),form.getStatus(),user.getUserId());
		model.addAttribute("pageModel", pageModel);
		
		setSellers(model,user.getUserId());
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/seller/order";
	}
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/order",method=RequestMethod.GET)
	public String order(Model model,HttpServletRequest req){
		
		Object object = req.getSession().getAttribute("user");
		if(null == object || !(object instanceof User)){
			model.addAttribute("message", "请首先登录系统！");
			return "/seller/failed";
		}
		User user = (User)object;
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllOrdersByUserId(1,"",0,"","",-1,user.getUserId());
		model.addAttribute("pageModel", pageModel);
		
		setSellers(model,user.getUserId());
		
		//初始化查询对象
		model.addAttribute("searchForm", new OrderSearchForm());
		
		return "/seller/order";
	}
	
	private PageModel getAllOrdersByUserId(int pageNum,String search,long sellerId,String start,String end,int status,long userId){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);

		try {
			return orderService.getPagedOrders(model, search, userId, sellerId, start, end, status,Constant.TYPE_BOOK_FOOD);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * @return
	 */
	@RequestMapping(value="/order",method=RequestMethod.POST)
	public String updateOrder(
			HttpServletRequest req,@ModelAttribute("form") BookOrder order,Model model){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				model.addAttribute("message", "请首先登录系统！");
				return "/seller/failed";
			}
			
			orderService.updateBookOrder(order);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/seller/failed";
		}
		
		return "redirect:/seller/order";
	}
}
